home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2001 May / SGI Freeware 2001 May - Disc 1.iso / dist / fw_mysql.idb / usr / freeware / share / sql-bench / test-select.z / test-select
Encoding:
Text File  |  1999-10-18  |  7.4 KB  |  271 lines

  1. #!/bin/perl5
  2. #
  3. # Test of selecting on keys that consist of many parts
  4. #
  5. ##################### Standard benchmark inits ##############################
  6.  
  7. use DBI;
  8. use Getopt::Long;
  9. use Benchmark;
  10.  
  11. $opt_loop_count=10000;
  12. $small_loop_count=10;
  13. $opt_regions=6;
  14. $opt_groups=100;
  15.  
  16. chomp($pwd = `pwd`); $pwd = "." if ($pwd eq '');
  17. require "$pwd/bench-init.pl" || die "Can't read Configuration file: $!\n";
  18.  
  19. $columns=min($limits->{'max_columns'},500,($limits->{'query_size'}-50)/24,
  20.          $limits->{'max_conditions'}/2-3);
  21.  
  22. if ($opt_small_test)
  23. {
  24.   $opt_loop_count/=10;
  25.   $opt_small_loop_count/=10;
  26.   $opt_groups/=10;
  27. }
  28.  
  29. print "Testing the speed of selecting on keys that consist of many parts\n";
  30. print "The test-table has $opt_loop_count rows and the test is done with $columns ranges.\n\n";
  31.  
  32. ####
  33. ####  Connect and start timeing
  34. ####
  35.  
  36. $dbh = $server->connect();
  37. $start_time=new Benchmark;
  38.  
  39. ####
  40. #### Create needed tables
  41. ####
  42.  
  43. goto select_test if ($opt_skip_create);
  44.  
  45. print "Creating table\n";
  46. $dbh->do("drop table bench1");
  47.  
  48. do_many($dbh,$server->create("bench1",
  49.                  ["region char(1) NOT NULL",
  50.                   "idn integer(6) NOT NULL",
  51.                   "rev_idn integer(6) NOT NULL",
  52.                   "grp integer(6) NOT NULL"],
  53.                  ["primary key (region,idn)",
  54.                   "unique (region,rev_idn)",
  55.                   "unique (region,grp,idn)"]));
  56. if ($opt_lock_tables)
  57. {
  58.   do_query($dbh,"LOCK TABLES bench1 WRITE");
  59. }
  60.  
  61. if ($opt_fast && $opt_server eq "pg")
  62. {
  63.   $server->vacuum($dbh);
  64. }
  65.  
  66. ####
  67. #### Insert $opt_loop_count records with
  68. #### region:    "A" -> "E"
  69. #### idn:     0 -> count
  70. #### rev_idn:    count -> 0,
  71. #### grp:    distributed values 0 - > count/100
  72. ####
  73.  
  74. print "Inserting $opt_loop_count rows\n";
  75.  
  76. $loop_time=new Benchmark;
  77. $query="insert into bench1 values (";
  78. $half_done=$opt_loop_count/2;
  79. for ($id=0,$rev_id=$opt_loop_count-1 ; $id < $opt_loop_count ; $id++,$rev_id--)
  80. {
  81.   $grp=$id*3 % $opt_groups;
  82.   $region=chr(65+$id%$opt_regions);
  83.   do_query($dbh,"$query'$region',$id,$rev_id,$grp)");
  84.   if ($id == $half_done)
  85.   {                # Test with different insert
  86.     $query="insert into bench1 (region,idn,rev_idn,grp) values (";
  87.   }
  88. }
  89.  
  90. $end_time=new Benchmark;
  91. print "Time to insert ($opt_loop_count): " .
  92.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  93.  
  94. if ($opt_fast && $opt_server eq "pg")
  95. {
  96.   $server->vacuum($dbh);
  97. }
  98.  
  99. ####
  100. #### Do some selects on the table
  101. ####
  102.  
  103. select_test:
  104.  
  105. print "Testing big selects on the table\n";
  106. $loop_time=new Benchmark;
  107. $rows=0;
  108. for ($i=0 ; $i < $small_loop_count ; $i++)
  109. {
  110.   $grp=$i*11 % $opt_groups;
  111.   $region=chr(65+$i%($opt_regions+1));    # One larger to test misses
  112.   $rows+=fetch_all_rows($dbh,"select idn from bench1 where region='$region'");
  113.   $rows+=fetch_all_rows($dbh,"select idn from bench1 where region='$region' and idn=$i");
  114.   $rows+=fetch_all_rows($dbh,"select idn from bench1 where region='$region' and rev_idn=$i");
  115.   $rows+=fetch_all_rows($dbh,"select idn from bench1 where region='$region' and grp=$grp");
  116.   $rows+=fetch_all_rows($dbh,"select idn from bench1 where region>='B' and region<='C' and grp=$grp");
  117.   $rows+=fetch_all_rows($dbh,"select idn from bench1 where region>='B' and region<='E' and grp=$grp");
  118.   $rows+=fetch_all_rows($dbh,"select idn from bench1 where grp=$grp"); # This is hard
  119. }
  120. $count=$small_loop_count*7;
  121.  
  122. $end_time=new Benchmark;
  123. print "Time for select_big ($count:$rows): " .
  124.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  125.  
  126. # Test select with many OR's
  127.  
  128. $loop_time=new Benchmark;
  129. $tmpvar=0;
  130. $count=0;
  131. $estimated=0;
  132. $max_and_conditions=$limits->{'max_conditions'}/2;
  133. $rows=0;
  134.  
  135. for ($i=0 ; $i < $small_loop_count ; $i++)
  136. {
  137.   $region=chr(65+$i%($opt_regions+1));    # One larger to test out-of-regions
  138.   $query="select * from bench1 where ";
  139.   $or_part="grp = 1";
  140.   $or_part2="region='A' and grp=1";
  141.  
  142.   for ($j=1 ; $j < $columns; $j++)
  143.   {
  144.     $tmpvar^= ((($tmpvar + 63) + $j)*3 % 100000);
  145.     $tmp=$tmpvar % $opt_groups;
  146.     $tmp_region=chr(65+$tmpvar%$opt_regions);
  147.     $or_part.=" or grp=$tmp";
  148.     if ($j < $max_and_conditions)
  149.     {
  150.       $or_part2.=" or region='$tmp_region' and grp=$tmp";
  151.     }
  152.   }
  153.   $or_part="region='$region' and ($or_part)";
  154.  
  155. # Same query, but use 'func_extra_in_num' instead.
  156.   if ($limits->{'func_extra_in_num'})
  157.   {
  158.     $in_part=$or_part;
  159.     $in_part=~ s/ = / IN \(/;
  160.     $in_part=~ s/ or grp=/,/g;
  161.     $in_part.= ")";
  162.     defined($found=fetch_all_rows($dbh,$query . $in_part)) || die $DBI::errstr;
  163.     $rows+=$found;
  164.     $count++;
  165.   }
  166.   for ($j=0; $j < 10 ; $j++)
  167.   {
  168.     $rows+=fetch_all_rows($dbh,$query . $or_part);
  169.     $rows+=fetch_all_rows($dbh,$query . $or_part2);
  170. # Do it a little harder by setting a extra range
  171.     $rows+=fetch_all_rows($dbh,"$query ($or_part) and idn < 50");
  172.     $rows+=fetch_all_rows($dbh,"$query (($or_part) or (region='A' and grp < 10)) and region <='B'")
  173.   }
  174.   $count+=$j*4;
  175.   $end_time=new Benchmark;
  176.   last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
  177.                      $small_loop_count));
  178. }
  179.  
  180. if ($estimated)
  181. { print "Estimated time"; }
  182. else
  183. { print "Time"; }
  184. print " for select_range ($count:$rows): " .
  185.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  186.  
  187. #
  188. # Testing MIN() and MAX() on keys
  189. #
  190.  
  191. if ($limits->{'group_functions'})
  192. {
  193.   $loop_time=new Benchmark;
  194.   $count=0;
  195.   $estimated=0;
  196.   for ($tests=0 ; $tests < $opt_loop_count ; $tests++)
  197.   {
  198.     $count+=7;
  199.     $grp=$tests*3 % $opt_groups;
  200.     $region=chr(65+$tests % $opt_regions);
  201.     if ($limits->{'group_func_sql_min_str'})
  202.     {
  203.       fetch_all_rows($dbh,"select min(region) from bench1");
  204.       fetch_all_rows($dbh,"select max(region) from bench1");
  205.       fetch_all_rows($dbh,"select min(region),max(region) from bench1");
  206.     }
  207.     fetch_all_rows($dbh,"select min(rev_idn) from bench1 where region='$region'");
  208.     fetch_all_rows($dbh,"select max(grp) from bench1 where region='$region'");
  209.     fetch_all_rows($dbh,"select max(idn) from bench1 where region='$region' and grp=$grp");
  210.     if ($limits->{'group_func_sql_min_str'})
  211.     {
  212.       fetch_all_rows($dbh,"select max(region) from bench1 where region<'$region'");
  213.     }
  214.     $end_time=new Benchmark;
  215.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,
  216.                        $tests+1, $opt_loop_count));
  217.   }
  218.   if ($estimated)
  219.   { print "Estimated time"; }
  220.   else
  221.   { print "Time"; }
  222.   print " for min_max_on_key ($count): " .
  223.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  224.  
  225.   $loop_time=new Benchmark;
  226.   $count=0;
  227.   $estimated=0;
  228.   for ($tests=0 ; $tests < $opt_loop_count ; $tests++)
  229.   {
  230.     $count+=5;
  231.     $grp=$tests*3 % $opt_groups;
  232.     $region=chr(65+$tests % $opt_regions);
  233.     fetch_all_rows($dbh,"select count(*) from bench1 where region='$region'");
  234.     fetch_all_rows($dbh,"select count(*) from bench1 where region='$region' and grp=$grp");
  235.     fetch_all_rows($dbh,"select count(*) from bench1 where region>'$region'");
  236.     fetch_all_rows($dbh,"select count(*) from bench1 where region<='$region'");
  237.     fetch_all_rows($dbh,"select count(*) from bench1 where region='$region' and grp>$grp");
  238.     $end_time=new Benchmark;
  239.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,
  240.                        $tests+1, $opt_loop_count));
  241.   }
  242.   if ($estimated)
  243.   { print "Estimated time"; }
  244.   else
  245.   { print "Time"; }
  246.   print " for count_on_key ($count): " .
  247.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  248. }
  249.  
  250. ####
  251. #### End of benchmark
  252. ####
  253.  
  254. if ($opt_lock_tables)
  255. {
  256.   do_query($dbh,"UNLOCK TABLES");
  257. }
  258. if (!$opt_skip_delete)
  259. {
  260.   do_query($dbh,"drop table bench1");
  261. }
  262.  
  263. if ($opt_fast && $opt_server eq "pg")
  264. {
  265.   $server->vacuum($dbh);
  266. }
  267.  
  268. $dbh->disconnect;                # close connection
  269.  
  270. end_benchmark($start_time);
  271.